<?php
/**
 * Created by PhpStorm.
 * User: dungnt13
 * Date: 12/12/14
 * Time: 3:28 PM
 */
namespace Frontend;

use BaseController;
use Input;
use Jinput;
use Response;
use View;
use Redirect;
use Str;
use Sentry;
use Lesson;
use Category;
use DInput;
use DB;
use User;

class CoursesController extends BaseController
{
    public function getIndex()
    {
        $data = array();
        return View::make('frontend/courses/index', compact('data'));
    }

    public function getDetails($id, $slug)
    {
        $data = array();
        $level = $data['level'] = Dinput::get('level', '', 'number');

        if (is_null($data['category'] = Category::find($id)))
            \App::abort(404);

        if (!is_numeric($level) || $level == '' || $level < 0 || $level > 10) {
            // get level under course
            $data['course_level'] = $data['category']->levels()->leftJoin('categories_lessons as c_l', 'c_l.category_id', '=', 'categories.id')
                ->select('categories.*', DB::raw('count(c_l.id) as total_lesson'))
                ->groupBy('categories.id')->orderBy('level', 'ASC')->get();
            $arrLevels = array();
            foreach ($data['course_level'] as $c) {
                $arrLevels[] = $c->id;
            }
            // get guru have lesson in course
            $data['gurus'] = User::join('lessons', 'lessons.user_id', '=', 'users.id')
                ->join('categories_lessons as c_l', 'c_l.lesson_id', '=', 'lessons.id')
                ->whereIn('c_l.category_id', $arrLevels)
                ->groupBy('users.id')
                ->select('users.*', DB::raw('count(lessons.id) as total_lesson'))
                ->get();
            // get user in course
            $data['students'] = User::join('users_categories', 'users_categories.user_id', '=', 'users.id')
                ->whereIn('users_categories.category_id', $arrLevels)
                ->orderBy(DB::raw('RAND()'))->paginate(20);
            // get top questions in course
            $data['questions'] = $data['category']->questions()
                ->orderBy('views', 'DESC')->take(10)->get();

            return View::Make('frontend/courses/details', compact('data'));

        } else {
            $data['course'] = Category::where('parent_id', '=', $id)->where('level', '=', $level)->first();
            if (is_null($data['course']))
                \App::abort(404);
            // get list of lesson in this level
            $data['lessons'] = $data['course']->lessonsInCourse()->join('users', 'users.id', '=', 'lessons.user_id')
                ->select('username', 'lessons.*')->orderBy('pivot_position', 'ASC')->get();
//                ->paginate(20);
            // get list of user pass this level
            $data['passed'] = $data['course']->userCourse()
                ->whereNotNull('users_categories.video_id')
                ->wherePivot('status', '=', 'passed')
                ->paginate();

            // get list of hot video exam this level
            $data['hotVideos'] = $data['course']->examVideos()
                ->join('users', 'users.id', '=', 'videos.user_id')
                ->where('is_hot_exam', '=', 1)->orderBy('videos.id', 'DESC')
                ->select('videos.*', 'username')
                ->take(5)->get();

            //
            $data['in_progress'] = false;
            if (Sentry::check()) {
                if (!is_null(Sentry::getUser()->inCourses()->where('users_categories.category_id','=',$data['course']->id)->first()))
                    $data['in_progress'] = true;
            }
            return View::make('frontend/courses/level/index', compact('data'));
        }
    }
}